package cn.lg.soar.system.biz.modules.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.biz.modules.auth.entity.TemporaryPassword;
import cn.lg.soar.system.biz.modules.auth.model.TemporaryPasswordVo;
import cn.lg.soar.system.biz.modules.auth.service.impl.TemporaryPasswordServiceImpl;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * @date 2021/10/16
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Tag(name = "临时密码管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/temporary-password")
public class TemporaryPasswordController {

    @Autowired
    private TemporaryPasswordServiceImpl service;


    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated TemporaryPasswordVo data) {
        return HttpResult.<Serializable>success("创建成功")
                .setData(this.service.create(UserContext.getUserId(), data));
    }

    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated TemporaryPasswordVo data) {
        this.service.update(UserContext.getUserId(), data);
        return HttpResult.success("更新成功");
    }

    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        AssertUtil.isTrue(service.removeByIds(ids), "删除失败");
        return HttpResult.success("删除成功");
    }

    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<TemporaryPassword> detail(@RequestParam(name = "id") @Id Long id) {
        return HttpResult.data(this.service.getById(id));
    }

    @ListMapping
    public HttpResult<List<TemporaryPassword>> list(){
        return HttpResult.data(service.listByUserId(UserContext.getUserId()));
    }

}
